﻿using System.Windows.Controls;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        public static void SelectRight()
        {
            if (Globals.selectedEditor == null) return;

            //正常模式，选取公式。
            if (Globals.selectedEditor.SelectedExpression == null)
            {
                if (Globals.selectedEditor.RootPanel != null)
                {
                    Globals.selectedEditor.SelectedExpression
                        = Globals.selectedEditor.RootPanel;
                }

                return;
            }

            LunarPresentation.Expressions.Expression curEx
                = Globals.selectedEditor.SelectedExpression;

            if (curEx is Expressions.SubPanel)
            {
                Expressions.SubPanel subPanel = curEx as Expressions.SubPanel;
                Expressions.SubPanel rightPanel = subPanel.RightSubPanel;
                if (rightPanel != null)
                {
                    curEx.Editor.SelectedExpression = rightPanel;
                }
                else
                {
                    if (subPanel.ParentExpression != null)
                    {
                        curEx.Editor.SelectedExpression = subPanel.ParentExpression;
                    }
                }
                return;
            }

            if (curEx.Parent is StackPanel)
            {
                StackPanel spanel = curEx.Parent as StackPanel;

                if (spanel.Orientation == Orientation.Horizontal)
                {
                    int curIndex = spanel.Children.IndexOf(curEx);
                    if (curIndex < spanel.Children.Count - 1)
                    {
                        if (spanel.Children[curIndex + 1] is Expressions.Expression)
                        {
                            curEx.Editor.SelectedExpression = spanel.Children[curIndex + 1] as Expressions.Expression;
                        }
                    }
                    else
                    {
                        if (spanel.Parent is Border)
                        {
                            Border mainBorder = spanel.Parent as Border;
                            if (mainBorder.Parent is Expressions.SubPanel)
                            {
                                curEx.Editor.SelectedExpression = mainBorder.Parent as Expressions.SubPanel;
                                if ((curEx.Editor.SelectedExpression as Expressions.SubPanel).SkipSelect)
                                {
                                    SelectRight();
                                }
                            }
                        }
                    }
                }
                else
                {
                    //垂直子面板中
                    if (spanel.Parent is Border)
                    {
                        Border mainBorder = spanel.Parent as Border;
                        if (mainBorder.Parent is Expressions.SubPanel)
                        {
                            curEx.Editor.SelectedExpression = mainBorder.Parent as Expressions.SubPanel;
                            if ((curEx.Editor.SelectedExpression as Expressions.SubPanel).SkipSelect)
                            {
                                SelectRight();
                            }
                        }
                    }
                }
            }
        }
    }
}
